package com.angis.fx.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.TextView;

import com.angis.fx.activity.R;

public class MultipleAdapter2 extends BaseAdapter {

	private List<String> mDataList;
	private LayoutInflater mLayoutInflater;
	private Set<Integer> mPos;
	private Map<Integer,String> mCounts;
	private Map<Integer,String> mRemarks;
	private Map<Integer, ViewHolder> mHolder;
	private ViewHolder lHolder = null;

	public MultipleAdapter2(Activity pParamActivity) {
		super();
		mPos = new HashSet<Integer>();
		mCounts = new HashMap<Integer,String>();
		mRemarks = new HashMap<Integer,String>();
		this.mLayoutInflater = LayoutInflater.from(pParamActivity);
		mDataList = new ArrayList<String>();
		mHolder = new HashMap<Integer, ViewHolder>();
	}

	@Override
	public int getCount() {
		return mDataList.size();
	}

	public String getCount(int position) {
		return null;
	}

	public ViewHolder getHolder(int position) {
		return mHolder.get(position);
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public void setCheckedPositions(HashSet<Integer> pPos) {
		mPos = pPos;
	}
	
	public Set<Integer> getPosition(){
		return mPos;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			lHolder = new ViewHolder();
			convertView = mLayoutInflater.inflate(
					R.layout.multiple_dialog_list_item2, null);
			lHolder.mName = (TextView) convertView
					.findViewById(R.id.multiple_dialog_item_name);
			lHolder.mCheckText = (CheckedTextView) convertView
					.findViewById(R.id.multiple_dialog_item_ctv);
			lHolder.mCount = (EditText) convertView
					.findViewById(R.id.multiple_dialog_item_count_et);
			lHolder.mRemark = (EditText) convertView
					.findViewById(R.id.multiple_dialog_item_remark_et);
			lHolder.mCount.setInputType(InputType.TYPE_CLASS_NUMBER);
			lHolder.mCount.setFocusable(true);
			lHolder.mCount.setEnabled(true);
//			lHolder.mCount.setOnTouchListener(new OnTouchListener() {
//				public boolean onTouch(View v, MotionEvent event) {
//					lHolder.mCount.setInputType(InputType.TYPE_CLASS_NUMBER);
//					InputMethodManager m = (InputMethodManager) lHolder.mCount
//							.getContext().getSystemService(
//									Context.INPUT_METHOD_SERVICE);
//					m.toggleSoftInput(2, InputMethodManager.HIDE_NOT_ALWAYS);
//					return true;
//
//				}
//			});
			lHolder.mRemark.setFocusable(true);
			lHolder.mRemark.setEnabled(true);
//			lHolder.mRemark.setOnTouchListener(new OnTouchListener() {
//				public boolean onTouch(View v, MotionEvent event) {
//					lHolder.mRemark.setInputType(InputType.TYPE_CLASS_TEXT);
//					InputMethodManager m2 = (InputMethodManager) lHolder.mRemark
//							.getContext().getSystemService(
//									Context.INPUT_METHOD_SERVICE);
//					m2.toggleSoftInput(2, InputMethodManager.HIDE_NOT_ALWAYS);
//					return true;
//
//				}
//			});
			convertView.setTag(lHolder);
			if (null == mHolder.get(position)) {
				mHolder.put(position, lHolder);
			}
		} else {
			lHolder = (ViewHolder) convertView.getTag();
		}
		lHolder.mName.setText(mDataList.get(position));
		lHolder.mCheckText.setVisibility(View.VISIBLE);
		lHolder.mCheckText.setChecked(false);
		Iterator<Integer> lIt = mPos.iterator();
		while (lIt.hasNext()){
			if (lIt.next() == position){
				lHolder.mCheckText.setChecked(true);
				break;
			}
				
		}
		if(mCounts.get(position) != null){
			lHolder.mCount.setText(mCounts.get(position));
		} else {
			lHolder.mCount.setText("");
		}
		if(mRemarks.get(position) != null){
			lHolder.mRemark.setText(mRemarks.get(position));
		}else {
			lHolder.mRemark.setText("");
		}
		lHolder.mCheckText.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CheckedTextView lCheckTextView = ((CheckedTextView) v);
				if (lCheckTextView.isChecked()) {
					mPos.remove(position);
					lCheckTextView.setChecked(false);
//					lHolder.mCount.setEnabled(false);
//					lHolder.mRemark.setEnabled(false);
				} else if (!lCheckTextView.isChecked()) {
					mPos.add(position);
					lCheckTextView.setChecked(true);
//					lHolder.mCount.setEnabled(true);
//					lHolder.mRemark.setEnabled(true);
				}
			}
		});
		
		lHolder.mCount.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				EditText lText = (EditText) v;
				if (lText.getText()!=null){
					mCounts.put(position, lText.getText().toString());
				}
				
			}
		});
		
		lHolder.mRemark.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				EditText lText = (EditText) v;
				if (lText.getText()!=null){
					mRemarks.put(position, lText.getText().toString());
				}
				
			}
		});
		
		return convertView;
	}

	public class ViewHolder {

		TextView mName;
		CheckedTextView mCheckText;
		EditText mCount;
		EditText mRemark;

		public TextView getName() {
			return mName;
		}

		public CheckedTextView getCheckText() {
			return mCheckText;
		}

		public TextView getmName() {
			return mName;
		}

		public void setmName(TextView mName) {
			this.mName = mName;
		}

		public CheckedTextView getmCheckText() {
			return mCheckText;
		}

		public void setmCheckText(CheckedTextView mCheckText) {
			this.mCheckText = mCheckText;
		}

		public EditText getmCount() {
			return mCount;
		}

		public void setmCount(EditText mCount) {
			this.mCount = mCount;
		}

		public EditText getmRemark() {
			return mRemark;
		}

		public void setmRemark(EditText mRemark) {
			this.mRemark = mRemark;
		}

	}

	public void setData(List<String> pDataList) {
		this.mDataList = pDataList;
//		this.mPos = pPos;
	}

}
